﻿package com.thorqq.magictimer.action;

import android.content.Context;
import android.media.AudioManager;

import com.thorqq.magictimer.core.TAction;
import com.thorqq.magictimer.core.TimerAbs;
import com.thorqq.magictimer.util.Util;

/**
 * 音量调节：铃声、音乐、通知
 * 
 */
public class ActionVolume extends TAction
{
    private static final long serialVersionUID = 6389618782306130969L;

    protected final int PARAM_NUM = 3;
    
    private int mVolRing = -1;
    private int mVolMusic = -1;
    private int mVolNotification = -1;

    public ActionVolume(int id, int execOrder, String actionType, String param)
    {
        super(id, execOrder, actionType, param);
    }

    @Override
    public boolean run(Context context, TimerAbs timer)
    {
        AudioManager audioMgr = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
        
        if(mVolRing >= 0)
        {
            audioMgr.setStreamVolume(AudioManager.STREAM_RING, mVolRing, 0);
        }
        
        if(mVolMusic >= 0)
        {
            audioMgr.setStreamVolume(AudioManager.STREAM_MUSIC, mVolMusic, 0);
        }
        
        if(mVolNotification >= 0)
        {
            audioMgr.setStreamVolume(AudioManager.STREAM_NOTIFICATION, mVolNotification, 0);
        }

        return true;
    }

    @Override
    public String getName()
    {
        return "音量调节";
    }

    @Override
    public String getDescription()
    {
        return null;
    }
    
    public void setRingVolume(int v)
    {
        mVolRing = v;
    }
    
    public int getRingVolume()
    {
        return mVolRing;
    }
    
    public void setMusicVolume(int v)
    {
        mVolMusic = v;
    }
    
    public int getMusicVolume()
    {
        return mVolMusic;
    }
    
    public void setNotificationVolume(int v)
    {
        mVolNotification = v;
    }
    
    public int getNotificationVolume()
    {
        return mVolNotification;
    }

    public void setParam(int r, int m, int n)
    {
        if(r >= 0)
        {
            mVolRing = r;
        }
        
        if(m >= 0)
        {
            mVolMusic = m;
        }
        
        if(n >= 0)
        {
            mVolNotification = n;
        }

        this.paramToString();
    }

    @Override
    public boolean parseStringParam()
    {
        if(mParam == null)
        {
            return true;
        }
        
        String[] array = Util.splitParames(mParam);
        if (array.length != PARAM_NUM)
        {
            Util.log("TimeParam setParam error. param = " + mParam);
            return false;
        }

        mVolRing = Util.strToInt(array[0]);
        mVolMusic = Util.strToInt(array[1]);
        mVolNotification = Util.strToInt(array[2]);

        return true;
    }

    @Override
    public String paramToString()
    {
        StringBuffer sff = new StringBuffer();
        sff.append(Util.intToStr(mVolRing))
           .append(Util.PARAMES_HYPHEN).append(Util.intToStr(mVolMusic))
           .append(Util.PARAMES_HYPHEN).append(Util.intToStr(mVolNotification))
           ;

        return super.mParam = sff.toString();
    }

}
